// ==UserScript==
// @name         DMM商店
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        https://dmmsd88.co/
// @match        https://mgssd88.vip/
// @icon         https://www.google.com/s2/favicons?sz=64&domain=dmmsd88.co
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Your code here...
    function addTopButton() {
        let shandian = null;
        var aaaArr = document.querySelectorAll('.nav_li.nav_hot a');
        for (let i = 0; i < aaaArr.length; i++) {
            let aaa = aaaArr[i];
            if (/商店/g.test(aaa.textContent)){
                shandian = aaa.parentElement;
                break;
            }
        }

        if (shandian) {
            /* 读取按钮 */
            var bigImgBtn = document.createElement('button');
            bigImgBtn.id = "bigimg-btn";
            bigImgBtn.style.color = "#4242c7"
            bigImgBtn.style.marginRight = "10px";
            bigImgBtn.textContent = "大预览图";
            bigImgBtn.addEventListener('click', function (event){
               changeImgSize();
            });

            /* 读取按钮 */
            var coverBtn = document.createElement('button');
            coverBtn.id = "cover-btn";
            coverBtn.style.color = "#4242c7"
            coverBtn.style.marginRight = "10px";
            coverBtn.textContent = "大封面";
            coverBtn.addEventListener('click', function (event){
                changeCoverSize();
            });

            // 加入按钮
            shandian.insertAdjacentElement('afterEnd', bigImgBtn);
            shandian.insertAdjacentElement('afterEnd', coverBtn);
        }
    }

    function addTopButton_2(){
        // 创建一个新的div元素
        const fixedPanel = document.createElement("div");
        fixedPanel.id = "top-panel";
        //fixedPanel.classList.add("fixed-panel");
        fixedPanel.style = `
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            background-color: #333;
            color: #fff;
            text-align: center;
            padding: 10px 0;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.3);
            z-index: 1000; /* 确保面板在其他内容之上 */
        `;

        // 可选：为内容添加额外的margin-top，确保内容不被遮挡
        const content = document.querySelector(".container-fluid");
        if (content) {
            content.style.marginTop = "50px";
        }

        /* 读取按钮 */
        var bigImgBtn = document.createElement('button');
        bigImgBtn.id = "bigimg-btn";
        bigImgBtn.style.color = "#4242c7"
        bigImgBtn.style.marginRight = "10px";
        bigImgBtn.textContent = "大预览图";
        bigImgBtn.addEventListener('click', function (event){
            changeImgSize();
        });

        /* 读取按钮 */
        var coverBtn = document.createElement('button');
        coverBtn.id = "cover-btn";
        coverBtn.style.color = "#4242c7"
        coverBtn.style.marginRight = "10px";
        coverBtn.textContent = "大封面";
        coverBtn.addEventListener('click', function (event){
            changeCoverSize();
        });

        /* 读取按钮 */
        var jumpBtn = document.createElement('button');
        jumpBtn.id = "jump-btn";
        jumpBtn.style.color = "#4242c7"
        jumpBtn.style.marginRight = "10px";
        jumpBtn.textContent = "跳转JDB";
        jumpBtn.addEventListener('click', function (event){
            let det = document.querySelector('.detail_data_id td');
            if (!det) return;
            
            let detStr = det.textContent;
            let avcode = null

            let reg = /[a-zA-Z]{2,5}00[0-9]{2,5}/g;
            let match = detStr.match(reg);
            if (match && match[0]){
                avcode = match[0];
                avcode = avcode.replace("00", "-");
            }else {
                let reg2 = /[a-zA-Z]{2,5}-[0-9]{2,5}/g;
                let match2 = detStr.match(reg2);
                if (match2 && match2[0]){
                    avcode = match2[0];
                }
            }

            if (avcode){
                let jdbUrl = "https://javdb367.com/search?q=" + avcode + "&f=all";
                parseJdbUrl(jdbUrl);
            }
        });

        fixedPanel.appendChild(bigImgBtn);
        fixedPanel.appendChild(coverBtn);
        fixedPanel.appendChild(jumpBtn);
        // 将面板插入到body元素的第一个子节点之前
        document.body.insertBefore(fixedPanel, document.body.firstChild);
    }

    function changeCoverSize(){
        let covers = document.querySelectorAll('.thumbnail.result img');
        for (let i = 0; i < covers.length; i++) {
            let cover = covers[i];

            cover.style.width = "200px";
            cover.style.height = "280px";

            cover.parentElement.style.width = "200px";
        }
    }

    function changeImgSize(){
        var preImgEles = $(".sample_photo img");
        if (preImgEles && preImgEles.length > 0){
            // var style2 = document.createElement('style');
            // style2.innerHTML = `
            //   .img-2 {
            //         height: auto;
            //         max-width: 250%;
            //         width: 500px
            //     }
            // `;
            // document.body.appendChild(style2);

            for(var i=0; i<preImgEles.length; i++){
                var preImgEle = preImgEles[i];
                //console.log(preImgEle);

                //preImgEle.classList.add("img-2");
                preImgEle.src = preImgEle.parentNode.href;
                preImgEle.style = `
                    height: 480px;
                `;
            }
        }
    }

    function parseJdbUrl(targetUrl){
        let domain = getDomainFromUrl(targetUrl);

        fetch(targetUrl) // 请求用户指定的页面
            .then(response => response.text())
            .then(html => {
                console.log(html);
                const parser = new DOMParser();
                const doc = parser.parseFromString(html, 'text/html');
                const movieList = doc.getElementsByClassName('movie-list');
                if (movieList.length > 0) {
                    const firstElement = movieList[0];
                    const firstLink = firstElement.querySelector('a');
                    if (firstLink) {
                        const href = firstLink.getAttribute('href');

                        var fullUrl = "http://" + domain + href;

                        var newWindow = window.open(fullUrl, '_blank'); // 在新标签页中打开URL
                        // 可选：检查窗口是否成功打开
                        if (!newWindow) {
                            // 窗口可能被浏览器阻止了
                            alert('请允许浏览器打开新窗口或标签页。');
                        }
                    }
                }
            })
            .catch(err => console.error('Error fetching and parsing HTML', err));
    }

    function getDomainFromUrl(url) {
        // 创建一个新的URL对象
        const urlObj = new URL(url);
        // 使用hostname属性获取域名
        return urlObj.hostname;
    }

    addTopButton_2();

})();